﻿using System;
using System.Collections.Generic;
using System.Text;
using Manager.App_Code.IDAL;
using Manager.App_Code.DTO;
using System.Data;

namespace Manager.App_Code.DAL
{
    class LoaiPhongDAL:StandardConnectDB, ILoaiPhong
    {
        public List<DTO.LoaiPhongDTO> GetListLoaiPhong()
        {
            List<LoaiPhongDTO> listLoaiphong = new List<LoaiPhongDTO>();
            DataTable dt = this.DTExecuteSelect("Select * from LoaiPhong");
            foreach (DataRow dr in dt.Rows)
            {
                listLoaiphong.Add(ConvertObjectDataRowToLoaiPhongDTODataRow(dr));
            }

            return listLoaiphong;          

        }

        public DTO.LoaiPhongDTO GetLoaiPhongByID(string MaLoaiPhong)
        {
            LoaiPhongDTO loaiphong = new LoaiPhongDTO();

            Dictionary<string, object> paramlist = new Dictionary<string, object>();

            paramlist.Add("@MaLoaiPhong", MaLoaiPhong);

            DataTable dt = this.DTExecuteSelect("SP_LoaiPhong_GetByID", paramlist);

            loaiphong = ConvertObjectDataRowToLoaiPhongDTODataRow(dt.Rows[0]);

            return loaiphong;
        }

        public bool InsertLoaiPhong(DTO.LoaiPhongDTO loaiph)
        {
            Dictionary<string, object> paramlist = new Dictionary<string, object>();
            paramlist.Add("@MaLoaiPhong", loaiph.MaLoaiPhong);
            paramlist.Add("@TenLoaiPhong", loaiph.TenLoaiPhong);
            paramlist.Add("@GiaPhong", loaiph.GiaPhong);
            paramlist.Add("@SoNguoi", loaiph.SoNguoi);
            paramlist.Add("@GhiChu", loaiph.GhiChu);

            return ExecuteUpdate("SP_LoaiPhong_Insert", paramlist);
        }

        public bool UpdateLoaiPhong(DTO.LoaiPhongDTO loaiph)
        {
            Dictionary<string, object> paramlist = new Dictionary<string, object>();
            paramlist.Add("@MaLoaiPhong", loaiph.MaLoaiPhong);
            paramlist.Add("@TenLoaiPhong", loaiph.TenLoaiPhong);
            paramlist.Add("@GiaPhong", loaiph.GiaPhong);
            paramlist.Add("DonViTienTe", loaiph.DonViTienTe);
            paramlist.Add("SoNguoi", loaiph.SoNguoi);
            paramlist.Add("@GhiChu", loaiph.GhiChu);

            return ExecuteUpdate("SP_LoaiPhong_Update", paramlist);
        }

        public bool DeleteLoaiPhong(DTO.LoaiPhongDTO loaiph)
        {
            Dictionary<string, object> paramlist = new Dictionary<string, object>();
            paramlist.Add("@MaLoaiPhong", loaiph.MaLoaiPhong);
            return ExecuteUpdate("SP_LoaiPhong_Delete", paramlist);
        }

        public DTO.LoaiPhongDTO ConvertObjectDataRowToLoaiPhongDTODataRow(System.Data.DataRow dr)
        {
            if (dr != null)
            {
                LoaiPhongDTO loaiphong = new LoaiPhongDTO();

                loaiphong.MaLoaiPhong = dr["MaLoaiPhong"].ToString();

                loaiphong.TenLoaiPhong = dr["TenLoaiPhong"].ToString();

                if (dr["GiaPhong"].ToString() != "")
                    loaiphong.GiaPhong = double.Parse(dr["GiaPhong"].ToString());

                if (dr["DonViTienTe"].ToString() != "")
                {
                    ITienTe itiente = new TienTeDAL();
                    loaiphong.DonViTienTe = itiente.GetTienTeByID(dr["DonViTienTe"].ToString());

                }

                if (dr["SoNguoi"].ToString()!="")
                   loaiphong.SoNguoi = int.Parse(dr["SoNguoi"].ToString());

                loaiphong.GhiChu = dr["GhiChu"].ToString();

                return loaiphong;
            }
            else
                return null;
           
        }


        public List<LoaiPhongDTO> GetListLoaiPhongCountSoLuong_TinhTrang()
        {
             
            //List<DTO.LoaiPhongDTO> listloaiphong = new List<DTO.LoaiPhongDTO>
            
            
            //DataTable dt = this.DTExecuteSelect("Select * from LoaiPhong");

            //foreach (DataRow dr in dt.Rows)
            //{
            //    listloaiphong.Add(ConvertObjectDataRowToLoaiPhongDTODataRow(dr));
            //}

            //return listloaiphong;
            throw new NotImplementedException();

        }
    }
}
